package cn.wfu.rlrjds.app.repaire.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wfu.rlrjds.app.bean.RepaireBean;
import cn.wfu.rlrjds.app.bean.RepaireSearch;
import cn.wfu.rlrjds.app.repaire.domain.Repaire;
import cn.wfu.rlrjds.app.repaire.service.IRepaireService;
import cn.wfu.rlrjds.app.repaire.service.ipml.RepaireException;
import cn.wfu.rlrjds.app.repairman.domain.Repaireman;
import cn.wfu.rlrjds.app.user.domain.User;
import cn.wfu.rlrjds.frame.utils.Const;
import cn.wfu.rlrjds.utils.GsonUtils;
import cn.wfu.rlrjds.utils.ImageUrlUtil;
import cn.wfu.rlrjds.utils.UUIDUtil;

@Controller
@RequestMapping(value = "/repaire")
public class RepaireController {

	@Resource
	private IRepaireService repaireService;

	/**
	 * 通过用户id获取全部维修记录
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getAllRepaire", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getAllRepaire(HttpServletRequest request) {
		String uid = request.getParameter("uid");
		if (StringUtils.isNotBlank(uid)) {
			RepaireSearch repaireSearch = new RepaireSearch();
			repaireSearch.setUid(uid);
			List<RepaireBean> repaireBeans = this.repaireService
					.list(repaireSearch);
			return GsonUtils.toJson(repaireBeans);
		}
		return Const.NOT_FOUND;
	}

	/**
	 * 通过rid获取维修记录
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRepaireByRid", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getRepaireByRid(HttpServletRequest request) {
		String rid = request.getParameter("rid");
		if (StringUtils.isNotBlank(rid)) {
			RepaireSearch repaireSearch = new RepaireSearch();
			repaireSearch.setRid(rid);
			RepaireBean repaireBean = this.repaireService
					.findOne(repaireSearch);
			return GsonUtils.toJson(repaireBean);
		}
		return Const.NOT_FOUND;
	}

	/**
	 * 保存维修信息
	 * 
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(HttpServletRequest request) throws IOException {
		try {
			String jsonStr = request.getParameter("repaire");
			String uid = request.getParameter("uid");
			String basepath = request.getServletContext().getRealPath(
					ImageUrlUtil.IMAGE_BASE);
			String saveDir = basepath + "/" + uid + "/repaire";
			File file = new File(saveDir);

			// 判断文件是否存在不存在创建
			if (!file.exists()) {
				file.mkdirs();
			}
			if (StringUtils.isNotBlank(jsonStr) && StringUtils.isNotBlank(uid)) {

				RepaireBean repaireBean = GsonUtils.fromJson(jsonStr,
						RepaireBean.class);
				List<String> imgeStrs = repaireBean.getImgUrl();

				// 构建图片储存的路径
				List<String> imageURIs = ImageUrlUtil.getImageURIs(imgeStrs,
						uid, saveDir);

				// 完善repaire的信息
				repaireBean.setRepaire_time(new Date());

				Repaire repaire = new Repaire();
				repaireBean.setImgUrl(null);
				ConvertUtils.register(new DateConverter(null),
						java.util.Date.class);
				BeanUtils.copyProperties(repaire, repaireBean);
				repaire.setRid(UUIDUtil.uuid());
				// 除去保修物品中提示性字符串
				List<String> goods = repaireBean.getGoods();
				repaire.setGoods(GsonUtils.toJson(goods));
				repaire.setImgUrl(GsonUtils.toJson(imageURIs));
				User user = new User();
				user.setUid(uid);
				repaire.setUser(user);
				System.out.println(repaire);
				repaireService.save(repaire);
				return Const.SUCCESS;

			} else {
				throw new RuntimeException("传入的参数不存在");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Const.NOT_FOUND;
		}
	}

	/**
	 * 添加评价信息
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/addEvaluate")
	@ResponseBody
	public String addEvaluate(HttpServletRequest request) {
		String evaluate = request.getParameter("evaluate");
		String rid = request.getParameter("rid");
		Map<String, String> map = new HashMap<String, String>();
		map.put("rid", rid);
		map.put("evaluate", evaluate);
		this.repaireService.addEvaluate(map);
		return Const.SUCCESS;
	}

	/**
	 * 获取全部未维修记录
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/getAllRepaireByRepaireStatus", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getAllRepaireByRepaireStatus(HttpServletRequest request) {
		List<RepaireBean> repaireBeans = this.repaireService
				.getAllRepaireByRepaireStatus();
		return GsonUtils.toJson(repaireBeans);
	}

	/**
	 * 维修员获取维修记录
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/toRepair", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String toRepair(HttpServletRequest request) {
		try {
			String rid = request.getParameter("rid");
			String id = request.getParameter("id");
			Repaire repaire = new Repaire();
			repaire.setRid(rid);
			Repaireman repaireman = new Repaireman();
			repaireman.setId(id);
			repaire.setRepairman(repaireman);
			repaire.setGet_repaire_time(new Date());
			repaire.setRepaireStatus(1);
			this.repaireService.toRepair(repaire);
			return Const.SUCCESS;
		} catch (RepaireException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Const.NOT_FOUND;
		}
	}

	/**
	 * 获取全部维修记录
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/getAllRepaires", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getAllRepaires(HttpServletRequest request) {
		List<RepaireBean> repaireBeans = this.repaireService
				.getAllRepaireByRepaireStatus();
		return GsonUtils.toJson(repaireBeans);
	}

	/**
	 * 通过维修员id俩查找订单
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findRepaireById", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String findRepaireById(HttpServletRequest request) {
		String id = request.getParameter("id");
		RepaireSearch repaireSearch = new RepaireSearch();
		repaireSearch.setId(id);
		List<RepaireBean> repaireBeans = this.repaireService
				.list(repaireSearch);
		return GsonUtils.toJson(repaireBeans);
	}

	/**
	 * 完成订单
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/finishRepaire", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String finishRepaire(HttpServletRequest request) {
		String rid = request.getParameter("rid");
		String id = request.getParameter("id");
		Repaire repaire = new Repaire();
		repaire.setRid(rid);
		Repaireman repaireman = new Repaireman();
		repaireman.setId(id);
		repaire.setRepairman(repaireman);
		repaire.setOver_time(new Date());
		repaire.setRepaireStatus(2);
		repaire.setConfirmStatus("0");
		this.repaireService.finishRepaire(repaire);
		return Const.SUCCESS;
	}

	@RequestMapping(value = "/aaaa", produces = "text/html;charset=UTF-8")
	public String aaaa(HttpServletRequest request) {
		List<Integer> goods_type = this.repaireService.findTypeCount();
		List<Integer> apartmentGoodsCount = this.repaireService
				.findApartmentGoodsCount();
		List<Integer> apartmentNetCount = this.repaireService
				.findApartmentNetCount();
		List<Integer> schoolGoodsCount = this.repaireService
				.findSchoolGoodsCount();
		request.setAttribute("goods_type", goods_type);
		request.setAttribute("apartmentGoodsCount", apartmentGoodsCount);
		request.setAttribute("apartmentNetCount", apartmentNetCount);
		request.setAttribute("schoolGoodsCount", schoolGoodsCount);
		return "theme";
	}

}
